﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Search.TxMainSearchControl.TxSearchITxObjectCollectionModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;
using UiInternalControls.WPFControls.SearchControl;


namespace DnProcessSimulateCommands.Search.TxMainSearchControl;

internal class TxSearchITxObjectCollectionModel : TxSearchBaseCollectionModel
{
  private int _indexOfRemovedObject = -1;

  protected virtual void Dispose(bool disposing)
  {
    if (disposing)
      this.UnregisterDeleteEvents();
    base.Dispose(disposing);
  }

  public bool RemoveItem(ITxObject obj)
  {
    bool flag = false;
    foreach (TxSearchItemITxObject searchItemItxObject in (Collection<TxSearchItemBase>) this.Items)
    {
      if (searchItemItxObject.Object == obj)
      {
        this._indexOfRemovedObject = this.Items.IndexOf((TxSearchItemBase) searchItemItxObject);
        flag = this.Items.Remove((TxSearchItemBase) searchItemItxObject);
        if (!flag)
        {
          this._indexOfRemovedObject = -1;
          break;
        }
        break;
      }
    }
    return flag;
  }

  private void UnregisterDeleteEvents()
  {
    if (this.Items == null)
      return;
    foreach (TxSearchItemITxObject searchItemItxObject in (Collection<TxSearchItemBase>) this.Items)
      searchItemItxObject.Object.Deleted -= new TxObject_DeletedEventHandler(this.searchObj_Deleted);
  }

  private void ClearList()
  {
    this.UnregisterDeleteEvents();
    this.Items.Clear();
  }

  public virtual void Search(string str)
  {
    str = TxSearchModelUtils.CleanStringForSearch(str);
    this.ClearList();
    List<ITxObject> objectsList = TxSearchITxObjectCollectionModel.GenerateObjectsList();
    foreach (ITxObject itxObject in (IEnumerable<ITxObject>) (!string.IsNullOrEmpty(str) ? (IEnumerable<ITxObject>) objectsList.FindAll((Predicate<ITxObject>) (txObj => txObj.Name.ToLower().Contains(str))) : (IEnumerable<ITxObject>) objectsList).OrderBy<ITxObject, string>((Func<ITxObject, string>) (txObj => txObj.GetType().ToString())).ThenBy<ITxObject, string>((Func<ITxObject, string>) (txObj => txObj.Name)))
    {
      TxSearchItemITxObject searchItemItxObject = new TxSearchItemITxObject(itxObject);
      itxObject.Deleted += new TxObject_DeletedEventHandler(this.searchObj_Deleted);
      this.Items.Add((TxSearchItemBase) searchItemItxObject);
    }
  }

  private void searchObj_Deleted(object sender, TxObject_DeletedEventArgs args)
  {
    (sender as ITxObject).Deleted -= new TxObject_DeletedEventHandler(this.searchObj_Deleted);
    if (!this.RemoveItem(sender as ITxObject) || !(sender is TxWeldLocationOperation))
      return;
    (sender as TxWeldLocationOperation).Collection.ItemsAdded += new TxObjectCollection_ItemsAddedEventHandler(this.TxSearchITxObjectCollectionModel_ItemsAdded);
  }

  private void TxSearchITxObjectCollectionModel_ItemsAdded(
    object sender,
    TxObjectCollection_ItemsAddedEventArgs args)
  {
    if (this._indexOfRemovedObject >= 0 && this._indexOfRemovedObject <= this.Items.Count && ((Collection<ITxObject>) args.Items).Count == 1)
    {
      TxSearchItemITxObject searchItemItxObject = new TxSearchItemITxObject(((Collection<ITxObject>) args.Items)[0]);
      ((Collection<ITxObject>) args.Items)[0].Deleted += new TxObject_DeletedEventHandler(this.searchObj_Deleted);
      this.Items.Insert(this._indexOfRemovedObject, (TxSearchItemBase) searchItemItxObject);
    }
    (sender as ITxObjectCollection).ItemsAdded -= new TxObjectCollection_ItemsAddedEventHandler(this.TxSearchITxObjectCollectionModel_ItemsAdded);
  }

  private static bool IsProcessDesigner()
  {
    bool flag = false;
    TxApplicationEx txApplicationEx = new TxApplicationEx();
    if (txApplicationEx.IsNewInfra() && txApplicationEx.IsOnLine())
      flag = true;
    return flag;
  }

  private static List<ITxObject> GenerateObjectsList()
  {
    TxObjectList allDescendants1 = TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxRobot)));
    TxObjectList txObjectList = new TxObjectList();
    foreach (TxRobot txRobot in (Collection<ITxObject>) allDescendants1)
      ((TxObjectList<ITxObject>) txObjectList).AddRange((TxObjectList<ITxObject>) txRobot.RoboticPrograms);
    TxObjectList allDescendants2 = TxApplication.ActiveDocument.LogicalRoot.GetAllDescendants((ITxTypeFilter) new TxNoTypeFilter());
    TxObjectList allDescendants3 = TxApplication.ActiveDocument.MfgRoot.GetAllDescendants((ITxTypeFilter) new TxNoTypeFilter());
    TxObjectList allDescendants4 = TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxNoTypeFilter());
    TxObjectList allDescendants5 = TxApplication.ActiveDocument.OperationRoot.GetAllDescendants((ITxTypeFilter) new TxNoTypeFilter());
    TxObjectList source = allDescendants2;
    ((TxObjectList<ITxObject>) source).AddRange((TxObjectList<ITxObject>) allDescendants3);
    ((TxObjectList<ITxObject>) source).AddRange((TxObjectList<ITxObject>) allDescendants4);
    ((TxObjectList<ITxObject>) source).AddRange((TxObjectList<ITxObject>) allDescendants5);
    ((TxObjectList<ITxObject>) source).AddRange((TxObjectList<ITxObject>) txObjectList);
    if (!TxSearchITxObjectCollectionModel.IsProcessDesigner())
    {
      TxPlcProgramRoot programRootOrNull = TxApplication.ActiveDocument.PlcProgramRootOrNull;
      if (TxPlcProgramRoot.op_Inequality(programRootOrNull, (ITxObject) null))
      {
        TxObjectList signals = programRootOrNull.CurrentPlcProgram.GetSignals((ITxTypeFilter) new TxNoTypeFilter());
        ((TxObjectList<ITxObject>) source).AddRange((TxObjectList<ITxObject>) signals);
      }
    }
    List<ITxObject> itxObjectList = new List<ITxObject>((IEnumerable<ITxObject>) ((IEnumerable<ITxObject>) source).Distinct<ITxObject>().ToList<ITxObject>());
    List<ITxObject> objectsList = new List<ITxObject>();
    foreach (ITxObject itxObject in itxObjectList)
    {
      if (new TxObjectBehaviorEx(itxObject).IsPersistent() && !itxObject.IsTransient)
        objectsList.Add(itxObject);
    }
    return objectsList;
  }
}
